#include "ui.h"
#include "lvgl.h"
#include "lv_driver.h"
#include <Arduino.h>
#include "TFT_eSPI.h"

#include "app/calculator.h"
#include "app/calendar.h"
#include "app/game.h"
#include "app/environment.h"
#include "app/music.h"
#include "app/setting.h"
#include "app/vcard.h"
#include "app/weather.h"
 extern TFT_eSPI tft;


lv_obj_t *scr_tft ;
lv_obj_t *scr_eink ;
lv_obj_t *label_menu;

extern bool eink_scene_ready;
extern bool eink_flash_ready;

static void mainpage_create(void); 

void ui_create(void)
{
    
    lv_theme_t *th = lv_theme_mono_init(200, NULL);
    lv_theme_set_current(th);
    // lv_disp_set_default(lv_tft);
    scr_tft = lv_disp_get_scr_act(lv_tft);
    

    



    // lv_disp_set_default(lv_eink);
    scr_eink = lv_disp_get_scr_act(lv_eink);
    mainpage_create();

}


static void mainpage_create(void)
{

    //tft

    lv_obj_t *bg = lv_obj_create(scr_tft,NULL);
    lv_obj_set_size(bg,160,80);

    static lv_style_t style;
    lv_style_copy(&style, &lv_style_plain_color);
    style.body.grad_color = LV_COLOR_BLACK;
    style.body.main_color = LV_COLOR_BLACK;     
    lv_obj_set_style(bg,&style);

    label_menu = lv_label_create(scr_tft,NULL);
    lv_label_set_text(label_menu, "Menu");    
    
    static lv_style_t style_title;
    lv_style_copy(&style_title, &lv_style_plain_color);
    style_title.text.color = LV_COLOR_MAKE(0xb3,0xee,0x3a);//LV_COLOR_GREEN;
    style_title.text.font = &lv_font_roboto_22;
    lv_label_set_style(label_menu,LV_LABEL_STYLE_MAIN,&style_title);

    lv_obj_align(label_menu, scr_tft, LV_ALIGN_CENTER, 0, 0);




    //EINK
    LV_IMG_DECLARE(img_calculator_32);
    LV_IMG_DECLARE(img_weather_32);
    LV_IMG_DECLARE(img_game_32);
    LV_IMG_DECLARE(img_vcard_32);
    LV_IMG_DECLARE(img_music_32);
    LV_IMG_DECLARE(img_env_32);
    LV_IMG_DECLARE(img_calendar_32);
    LV_IMG_DECLARE(img_book_32); 
    LV_IMG_DECLARE(img_set_32);

    /*Create an Image button*/
    lv_obj_t * imgbtn1 = lv_imgbtn_create(scr_eink, NULL);
    lv_imgbtn_set_src(imgbtn1, LV_BTN_STATE_REL, &img_calculator_32);
    lv_imgbtn_set_src(imgbtn1, LV_BTN_STATE_PR, &img_calculator_32);
    lv_imgbtn_set_src(imgbtn1, LV_BTN_STATE_TGL_REL, &img_calculator_32);
    lv_imgbtn_set_src(imgbtn1, LV_BTN_STATE_TGL_PR, &img_calculator_32);
    
    
    


    lv_obj_t * imgbtn2 = lv_imgbtn_create(scr_eink, imgbtn1);   lv_imgbtn_set_src(imgbtn2, LV_BTN_STATE_REL, &img_weather_32);
    lv_obj_t * imgbtn3 = lv_imgbtn_create(scr_eink, imgbtn1);   lv_imgbtn_set_src(imgbtn3, LV_BTN_STATE_REL, &img_game_32);
    lv_obj_t * imgbtn4 = lv_imgbtn_create(scr_eink, imgbtn1);   lv_imgbtn_set_src(imgbtn4, LV_BTN_STATE_REL, &img_vcard_32);
    lv_obj_t * imgbtn5 = lv_imgbtn_create(scr_eink, imgbtn1);   lv_imgbtn_set_src(imgbtn5, LV_BTN_STATE_REL, &img_music_32); 
    lv_obj_t * imgbtn6 = lv_imgbtn_create(scr_eink, imgbtn1);   lv_imgbtn_set_src(imgbtn6, LV_BTN_STATE_REL, &img_env_32);
    lv_obj_t * imgbtn7 = lv_imgbtn_create(scr_eink, imgbtn1);   lv_imgbtn_set_src(imgbtn7, LV_BTN_STATE_REL, &img_calendar_32);
    lv_obj_t * imgbtn8 = lv_imgbtn_create(scr_eink, imgbtn1);   lv_imgbtn_set_src(imgbtn8, LV_BTN_STATE_REL, &img_book_32);
    lv_obj_t * imgbtn9 = lv_imgbtn_create(scr_eink, imgbtn1);   lv_imgbtn_set_src(imgbtn9, LV_BTN_STATE_REL, &img_set_32);
    
    lv_obj_align(imgbtn1, NULL, LV_ALIGN_IN_TOP_LEFT, 15, 15);
    lv_obj_align(imgbtn2, NULL, LV_ALIGN_IN_TOP_MID, 0, 15);
    lv_obj_align(imgbtn3, NULL, LV_ALIGN_IN_TOP_RIGHT, -15, 15);

    lv_obj_align(imgbtn4, imgbtn1, LV_ALIGN_OUT_BOTTOM_MID, 0, 20);
    lv_obj_align(imgbtn5, imgbtn2, LV_ALIGN_OUT_BOTTOM_MID, 0, 20);
    lv_obj_align(imgbtn6, imgbtn3, LV_ALIGN_OUT_BOTTOM_MID, 0, 20);
 
    lv_obj_align(imgbtn7, imgbtn4, LV_ALIGN_OUT_BOTTOM_MID, 0, 20);
    lv_obj_align(imgbtn8, imgbtn5, LV_ALIGN_OUT_BOTTOM_MID, 0, 20);
    lv_obj_align(imgbtn9, imgbtn6, LV_ALIGN_OUT_BOTTOM_MID, 0, 20);


    lv_obj_set_event_cb(imgbtn1, calculator_create);
    lv_obj_set_event_cb(imgbtn2, weather_create); 
    lv_obj_set_event_cb(imgbtn3, NULL);
    lv_obj_set_event_cb(imgbtn4, vcard_create);
    lv_obj_set_event_cb(imgbtn5, music_create); 
    lv_obj_set_event_cb(imgbtn6, environment_create); 
    lv_obj_set_event_cb(imgbtn7, calendar_create);
    lv_obj_set_event_cb(imgbtn8, NULL); 
    lv_obj_set_event_cb(imgbtn9, setting_create); 


}





